package com.yuyu.commons.redis.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * @author redis Config
 * @version 1.0
 * @description: TODO
 * @date 2022/8/2 10:46
 */
@Configuration
public class redisConfig {

    @Value("${spring.redis.host}")
    private String host;



    @Bean(name = "sysJedisPoolConfig")
    public JedisPoolConfig a() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(1000);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setNumTestsPerEvictionRun(1024);
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
        jedisPoolConfig.setMinEvictableIdleTimeMillis(1800000);
        jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000);
        jedisPoolConfig.setMaxWaitMillis(2000);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setTestWhileIdle(true);
        jedisPoolConfig.setBlockWhenExhausted(false);
        return jedisPoolConfig;
    }

    @Bean(name = "sysRedisConnectionFactory")
    public RedisConnectionFactory b(@Qualifier("sysJedisPoolConfig") JedisPoolConfig jedisPoolConfig) {
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
        redisConfig.setHostName(host);
        redisConfig.setPort(6379);
        redisConfig.setPassword(RedisPassword.of(""));
        redisConfig.setDatabase(1);

        JedisClientConfiguration.DefaultJedisClientConfigurationBuilder configurationBuilder = (JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration
                .builder();
        configurationBuilder.usePooling();
        configurationBuilder.poolConfig(jedisPoolConfig);

        configurationBuilder.readTimeout(Duration.ofMillis(1000));
//		configurationBuilder.connectTimeout(Duration.ofMillis(10000));
        JedisClientConfiguration clientConfiguration = configurationBuilder.build();
        RedisConnectionFactory redisConnectionFacotry = new JedisConnectionFactory(redisConfig, clientConfiguration);
        return redisConnectionFacotry;
    }

    @Bean(name = "sysRedisTemplate")
    public RedisTemplate<String, Object> redisTemplate(@Qualifier("sysRedisConnectionFactory") RedisConnectionFactory redisConnectionFacotry) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFacotry);
        // 使用fastjson序列化
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
//		template.afterPropertiesSet();
        return template;

    }
}
